#include "my_iic.h"
#include "i2cDriveS.h"


#define EGSS_ADDR	0xC2

sI2CTypedef_t EgssI2c={0};



//硬件接口
//SDA PD4
//SCL PD5

static void i2c_IoInit(void) {
    //io初始化输入
    GPIO_InitTypeDef GPIO_Initure;

    GPIO_Initure.Pin = GPIO_PIN_4 | GPIO_PIN_5;
    GPIO_Initure.OutputEn = DISABLE;
    GPIO_Initure.InputEn = ENABLE;
    GPIO_Initure.PullupEn = DISABLE;
    HAL_GPIO_Init(GPIOD, &GPIO_Initure);
}

static void i2c_SdaH(void) {
    HAL_GPIO_SetInOut(GPIOD, GPIO_PIN_4, ENABLE, DISABLE);
    HAL_GPIO_WritePin(GPIOD, GPIO_PIN_4, GPIO_PIN_RESET);
}

static void i2c_SdaL(void) {
    HAL_GPIO_SetInOut(GPIOD, GPIO_PIN_4, ENABLE, ENABLE);
    HAL_GPIO_WritePin(GPIOD, GPIO_PIN_4, GPIO_PIN_RESET);
}

static u8 i2c_ReadSda(void) {
    return HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_4);
}

static u8 i2c_ReadScl(void) {
    return HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_5);
}

static void i2c_callback(sI2C_CbTypedef_e i2c_CbType)
{
    u8 write_addr = 0;
    u8 send_addr = 0;
    u8 tmp_data;
    switch (i2c_CbType)
    {
        case IIC_S_IT_IDLE: { //空闲         
            siic.tx_index = 0;
            siic.rx_index = 0;
        }break;
        case IIC_S_IT_RECV: { //接收中断           
            siic.rx_buf[siic.rx_index] = EgssI2c.Value;  //从iic句柄中获取接收到的数据
            siic.rx_index++;
            if (siic.rx_index >= EGSS_RX_LEN) siic.rx_index = EGSS_RX_LEN - 1;	//防溢出

            if (siic.rx_index == 2) {		//接收到操作地址
                siic.addr = siic.rx_buf[1];
                siic.tx_index = 0;
            }
            else if (siic.rx_index > 2) {	//接收写入数据
                write_addr = siic.addr + siic.rx_index - 3;
                if (write_addr >= EGSS_REG_LEN) write_addr = EGSS_REG_LEN - 1;	//防溢出
                siic.reg[write_addr] = siic.rx_buf[siic.rx_index - 1];		    //写入到参数表  
            }
        }break;
        case IIC_S_IT_SEND: { //发送中断		
            send_addr = siic.addr + siic.tx_index;
            tmp_data = siic.reg[send_addr];
            siic.tx_index = siic.tx_index + 1;
            EgssI2c.Value = tmp_data;                    //将待发数据写入到iic句柄
        }break;
        case IIC_S_IT_PEND: { //主机回UACK
            siic.tx_index = 0;
        }break;
        default:
            break;
    }
}


void egss_i2c_init(sI2CTypedef_t* i2c) {
    i2c->DevAddrSet=EGSS_ADDR;
	i2c->IIC_IO_INIT = i2c_IoInit;
	i2c->IIC_SDA_H = i2c_SdaH;
	i2c->IIC_SDA_L = i2c_SdaL;
	i2c->IIC_READ_SDA = i2c_ReadSda;
	i2c->IIC_READ_SCL = i2c_ReadScl;
    i2c->IIC_CALLBACK=i2c_callback;
}

void egss_init(void)
{
    egss_i2c_init(&EgssI2c);
    EgssI2c.IIC_IO_INIT();
    siic.reg[0]=1;
    siic.reg[1]=2;
    siic.reg[2]=3;
    siic.reg[3]=4;
    siic.reg[4]=5;
    siic.reg[5]=6;
    siic.reg[6]=7;
    siic.reg[7]=8;
    siic.reg[8]=9;
    
}

//扫描io
void egss_iic_task(void)
{
    static u8 sda = 0;
    static u8 scl = 0;
    static u8 sda_old = 0;
    static u8 scl_old = 0;

    sda = HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_4);
    scl = HAL_GPIO_ReadPin(GPIOD, GPIO_PIN_5);

    if (sda_old != sda) {
        if (sda == 0) {
            i2c_Sda_L_Cb(&EgssI2c);      //等效sda下降沿中断
        }
        else {
            i2c_Sda_H_Cb(&EgssI2c);      //等效sda上升沿中断
        }
        sda_old = sda;
    }
    if (scl_old != scl) {
        if (scl == 0) {
            i2c_Scl_L_Cb(&EgssI2c);
        }
        else {
            i2c_Scl_H_Cb(&EgssI2c);
        }
        scl_old = scl;
    }
}


